<template>

  <el-dialog title="新增资料" :visible.sync="dialogFormVisible" width="500px">
    <div class="flex-colum-center">
      <el-form :model="form" ref="dialogForm" label-position="right" label-width="110px" :rules="rules">
        <el-form-item label="资料名称:" prop="dataName">
          <el-input style="width: 260px" v-model="form.dataName" placeholder="请输入资料名称" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="资料格式:" prop="dataFormat">
          <el-select v-model="form.dataFormat" clearable style="width: 260px" multiple>
            <el-option v-for="dict in dict.type.lr_files_type" :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数目(份):" prop="dataNum">
          <el-input-number v-model="form.dataNum" :min="1" :max="100"></el-input-number>
        </el-form-item>
        <el-form-item label="存储规格(M):" prop="dataSize">
          <el-input-number v-model="form.dataSize" :min="1" :max="100"></el-input-number>
        </el-form-item>
        <el-form-item label="关联业务:" prop="associatedBusiness" filterable>
          <el-select style="width: 260px" v-model="form.associatedBusiness" multiple>
            <el-option v-for="dict in businessType" :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联客户:" prop="associatedCustomer" filterable>
          <el-select style="width: 260px" v-model="form.associatedCustomer" multiple>
            <el-option v-for="dict in customerType" :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态:" prop="status">
            <e-switch v-model="form.status"></e-switch>
<!--          <el-switch style="width: 260px" v-model="form.status"  :active-value="1" :inactive-value="2" active-text="开启" inactive-text="停用"></el-switch>-->
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="saveSubmit">确 定</el-button>
    </div>
  </el-dialog>

</template>

<script>
import * as filesConfigApi from "@/api/letterReview/filesConfig";
import {filesConfigAdd} from "@/api/letterReview/filesConfig";

export default {
  dicts: ['lr_files_type'],
  props: [
    'businessType',
    'customerType'
  ],
  data() {
    return {
      dialogFormVisible: false,
      type: 1,//1新增 2编辑
      form: {
        id: null,
        dataName: null,
        dataFormat:null,
        dataNum:1,
        dataSize:1,
        associatedBusiness:null,
        associatedCustomer:null,
        status:1
      },
      rules: {
        dataName: [
          { required: true, message: '请输入资料名称', trigger: 'change' },
        ],
        dataFormat: [
          { required: true, message: '请选择资料格式', trigger: 'change' },
        ],
        dataNum: [
          { required: true, message: '请输入数目', trigger: 'change' },
        ],
        dataSize: [
          { required: true, message: '请输入存储规则', trigger: 'change' },
        ],
        associatedBusiness: [
          { required: true, message: '请选择关联业务', trigger: 'change' },
        ],
        associatedCustomer: [
          { required: true, message: '请选择关联客户', trigger: 'change' },
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' },
        ],

      }
    }
  },
  // 方法集合
  methods: {
    saveSubmit() {

      this.$refs['dialogForm'].validate((valid) => {
        if (valid) {
          this.form.dataFormat = this.form.dataFormat.join(',')
          this.form.associatedBusiness = this.form.associatedBusiness.join(',')
          this.form.associatedCustomer = this.form.associatedCustomer.join(',')
          if (this.form.id){
            filesConfigApi.filesConfigUpdate(this.form).then(res=>{
              this.close()
              this.$message.success('编辑成功')
              this.$emit('success')
            })
          }else {
            filesConfigApi.filesConfigAdd(this.form).then(res=>{
              this.close()
              this.$message.success('新增成功')
              this.$emit('success')
            })
          }
        }
      })


    },
    getDetail() {
      filesConfigApi.filesConfigDetail(this.form.id).then((res) => {
        this.form = res.data
        this.form.dataFormat = this.form.dataFormat.split(',')
        this.form.associatedBusiness = this.form.associatedBusiness.split(',')
        this.form.associatedCustomer = this.form.associatedCustomer.split(',')
      })
    },
    show(type, id) {
      this.type = type
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dialogForm.resetFields()
        if (this.type == 2) {
          this.form.id = id
            this.getDetail()
        }else {
            this.form = {
              id: null,
              dataName: null,
              dataFormat:null,
              dataNum:1,
              dataSize:1,
              associatedBusiness:null,
              associatedCustomer:null,
              status:1
            }
        }
      })
    },
    close() {
      this.dialogFormVisible = false
    }
  },
  created() {
  },
  mounted() {
  },

}
</script>

<style lang="scss" scoped>
</style>
